package com.craney.aboutDispatcher
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class Main
	{
		
		
		public function Main()
		{
//			testEvenDispatcher();
//			testExtendDispatcher();
//			testCompDispatcher();
			testInterfaceDispatcher();
		}
		
		private function testInterfaceDispatcher():void
		{
			var intDisp:InterfaceDispatcher = new InterfaceDispatcher();
			intDisp.addEventListener("Interface", InterfaceHandler);
			intDisp.dispatchEvent(new NewEvent("Interface"));
		}
		
		private function testCompDispatcher():void
		{
			var comDisp:CompDispatcher = new CompDispatcher();
			comDisp.addListener("Comp", handler);
			comDisp.dispatchEvent(new NewEvent("Comp"));
		}
		
		
		
		private function testExtendDispatcher():void
		{
			var extDisp:ExtendDispatcher = new ExtendDispatcher();
			
			extDisp.addEventListener(NewEvent.JustinEvent, handler);
			extDisp.dispatchEvent(new NewEvent(NewEvent.JustinEvent));
			
			//测试定义的监听和发送
//			trace(extDisp.myAddEventListener(handler));
//			trace(extDisp.myDispatch());
		}
		
		
		private function testEvenDispatcher():void
		{
			var disp:EventDispatcher = new EventDispatcher();
			
			disp.addEventListener(NewEvent.CraneyEvent, handler);
			disp.dispatchEvent(new NewEvent(NewEvent.CraneyEvent));
//			disp.dispatchEvent(new Event(NewEvent.CraneyEvent));
			
		}
		
		private function InterfaceHandler(evt:Event):void
		{
			if((evt as NewEvent).myTarget is InterfaceDispatcher)
			{
				var id:InterfaceDispatcher = (evt as NewEvent).myTarget as InterfaceDispatcher;
				trace(id);
				trace("Successful!");	
			}
			else
			{
				trace("No");
			}
		}
		
		
		private function handler(evt:Event):void
		{
			if(evt is NewEvent)
			{
				var ne:NewEvent = evt as NewEvent;
				trace("This is NewEvent");
				trace(ne.type, "Event");
				trace("target=" + ne.target);
				trace("ne=" + ne);
				ne.show();
			}
			else
			{
				trace("Maybe Wrong! This is the original Event");
				trace("evt" + evt);
				trace(evt.type, "Event");
				trace("target=" + evt.target);
			}
		}
		

	}
}